package bookshopSM;

import java.sql.*;
public class DBConnection{
    static  Connection conn = null;
    private static String username ="root";
    private static String password="root" ;
    private static String driverClassName="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3307/books_system";
    public  Connection getConnection()  {
        try {
            Class.forName(driverClassName); //或者Class.forName(driverClassName)...newInstance();
            conn = DriverManager.getConnection(url,username,password);
            System.out.println("成功");
        }
        catch(Exception e){
            e.printStackTrace();
            System.out.println("失败");
        }
        return conn;
    }
    public void closeConnection() throws SQLException {
        if (conn!= null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}



